package com.account.controller;

import java.io.UnsupportedEncodingException;

import javax.annotation.Resource;

import org.springframework.stereotype.Controller;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import com.account.controller.base.BaseController;
import com.account.dto.AccountDto;
import com.account.service.AccountService;
import com.account.util.BaseResult;
import com.account.util.Constants;
import com.account.util.Pager;

@Controller
@RequestMapping(value = "/account")
public class AccountController extends BaseController {

  @Resource
  private AccountService accountService;

  /**
   * 添加帐目信息
   * @param dto
   * @throws UnsupportedEncodingException 
   */
  @ResponseBody
  @RequestMapping(value = "/add-account-info")
  public BaseResult<String> addAccountInfo(@RequestBody AccountDto dto) throws UnsupportedEncodingException {
    BaseResult<String> baseResult = new BaseResult<String>();
    if (dto == null || dto.getType() == null || dto.getMoney() == null
        || StringUtils.isEmpty(dto.getComment().trim())) {
      baseResult.setCode(Constants.CODE_ERROR);
      baseResult.setMessage("参数错误！");
      return baseResult;
    }
    dto.setComment(new String(dto.getComment().getBytes("ISO-8859-1"), "UTF-8"));
    // 添加帐目信息
    Boolean result = accountService.addAccountInfo(dto);
    if (result) {
      baseResult.setCode(Constants.CODE_SUCCESS);
      baseResult.setData("添加成功！");
    } else {
      baseResult.setCode(Constants.CODE_ERROR);
      baseResult.setMessage("添加失败！");
    }
    return baseResult;
  }
  
  /**
   * 分页查询帐目信息列表信息
   * @param dto
   * @return
   */
  @ResponseBody
  @RequestMapping(value = "/get-account-info-list")
  public BaseResult<Pager<AccountDto>> getAccountInfoList(@RequestBody AccountDto dto){
    BaseResult<Pager<AccountDto>> baseResult = new BaseResult<Pager<AccountDto>>();
    if(dto.getPageSize() == null){
      dto.setPageSize(10l);
    }
    if(dto.getPageNumber() == null){
      dto.setPageNumber(1l);
    }
    Pager<AccountDto> pager = accountService.getAccountInfoList(dto);
    baseResult.setCode(Constants.CODE_SUCCESS);
    baseResult.setData(pager);
    return baseResult;
  }

}
